package com.hs.communication.common.fallback;

import com.hs.common.core.domain.Result;
import com.hs.communication.common.entity.Charger;
import com.hs.communication.common.entity.ChargerData;
import com.hs.communication.common.entity.vo.DeviceVo;
import com.hs.communication.common.feign.RemoteChargerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class RemoteChargerFallbackFactory implements FallbackFactory<RemoteChargerService> {

    @Override
    public RemoteChargerService create(Throwable cause) {
        log.error("获取充电桩统计数据远程服务调用失败:{}", cause.getMessage());
        return new RemoteChargerService() {
            @Override
            public Result<DeviceVo> getStatistics(long userId, String source) {
                return Result.fail(DeviceVo.builder().build(),"获取充电桩统计数据降级回调");
            }

            @Override
            public Result<List<String>> getSns(long userId, String source) {
                return Result.fail(new ArrayList<>(),"获取SN列表降级回调");
            }

            @Override
            public Result<Boolean> insert(ChargerData chargerData, String source) {
                System.out.println("插入实时数据失败,触发降级");
                return Result.fail(false,"插入充电桩实时数据降级回调");
            }

            @Override
            public Result<Boolean> updateStatus(Charger charger, String source) {
                System.out.println("更改在线/离线状态失败sn=" + charger.getSn());
                return Result.fail(false,"更改充电桩在线/离线状态降级回调");
            }
        };
    }
}
